package com.zzqstart.cateservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzqstart.cateservice.controller.remote.consume.UcenRemoteController;
import com.zzqstart.cateservice.entity.CateComment;
import com.zzqstart.cateservice.entity.CommentSearch;
import com.zzqstart.cateservice.mapper.CateCommentMapper;
import com.zzqstart.cateservice.service.CateCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzqstart.cateservice.service.CateRecipeService;
import com.zzqstart.commonutils.entity.UcenterMember;
import com.zzqstart.servicebase.exceptionhandler.CateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@Service
public class CateCommentServiceImpl extends ServiceImpl<CateCommentMapper, CateComment> implements CateCommentService {

    @Autowired
    private UcenRemoteController ucenRemoteController;

    @Transactional
    @Override
    public Map<String, Object> getCommentByCondition(CommentSearch searchObj, Long current, Long limit) {
        Page<CateComment> page = new Page<>(current, limit);

        QueryWrapper<CateComment> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(searchObj)){

            if (!StringUtils.isEmpty(searchObj.getRecipeId())){
                wrapper.eq("recipe_id",searchObj.getRecipeId());
            }

            if (!StringUtils.isEmpty(searchObj.getRecipeTitle())){
                wrapper.like("recipe_title",searchObj.getRecipeTitle());
            }

            if (!StringUtils.isEmpty(searchObj.getPublishId())){
                wrapper.eq("publish_id",searchObj.getPublishId());
            }

            if (!StringUtils.isEmpty(searchObj.getNickname())){
                wrapper.eq("nickname",searchObj.getNickname());
            }

            if (!StringUtils.isEmpty(searchObj.getMemberId())){
                wrapper.eq("member_id",searchObj.getMemberId());
            }

            if (!StringUtils.isEmpty(searchObj.getContent())){
                wrapper.like("content",searchObj.getContent());
            }

            if (!StringUtils.isEmpty(searchObj.getTime())){
                wrapper.le("create_time",searchObj.getTime());
            }

        }
        wrapper.orderByDesc("create_time");
        baseMapper.selectPage(page,wrapper);

        List<CateComment> list = page.getRecords();
        long total = page.getTotal();
        long currentPage = page.getCurrent();

        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        map.put("currentPage",currentPage);

        return map;
    }

    @Override
    public Map<String, Object> getCommentByRecipeId(String recipeId, Long current, Long limit) {

        Page<CateComment> page = new Page<>(current, limit);

        QueryWrapper<CateComment> wrapper = new QueryWrapper<>();
        wrapper.eq("recipe_id",recipeId);
        wrapper.orderByDesc("create_time");
        baseMapper.selectPage(page,wrapper);

        List<CateComment> list = page.getRecords();
        long total = page.getTotal();
        long currentPage = page.getCurrent();

        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        map.put("currentPage",currentPage);

        return map;
    }

    @Override
    public boolean sendComment(CateComment cateComment, String memberId) {
        UcenterMember member = ucenRemoteController.getMemberInfoByMemberId(memberId);
        BeanUtils.copyProperties(member,cateComment);
        cateComment.setMemberId(memberId);
        cateComment.setId("");

        int insert = baseMapper.insert(cateComment);

        return insert>0;
    }

    @Override
    public boolean deletedComment(String memberId, String commentId) {
        QueryWrapper<CateComment> wrapper = new QueryWrapper<>();
        wrapper.eq("id",commentId);

        CateComment comment = baseMapper.selectOne(wrapper);

        if (StringUtils.isEmpty(comment)){
            throw new CateException(20001,"不存在这条评论");
        }

        if (!memberId.equals("admin")){
            if (!comment.getPublishId().equals(memberId) && !comment.getMemberId().equals(memberId) ){
                throw new CateException(20001,"您没有权限删除该数据");
            }
        }

        int delete = baseMapper.delete(wrapper);
        return delete>0;
    }


}
